package com.freewater.springboot.demo.ioc.qualifier;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Map;

@Getter
@Setter
public class MovieRecommender implements BeanNameAware {

    private String beanName;

    @Autowired
    @Qualifier("action")
    private Map<String, MovieCatalog> movieCatalogsMap1;// qualifier 为action的有两个bean，movieCatalog1, movieCatalog2

    @Autowired
    @Qualifier("main")
    private Map<String, MovieCatalog> movieCatalogsMap2;// qualifier 为main的有两个bean，movieCatalog3, movieCatalog4

    @Autowired
    @Qualifier("action")
    private MovieCatalog movieCatalog1; // qualifier 为action的有两个bean，但是字段名movieCatalog1能够再次区分，所以注入成功

    @Autowired
    @Qualifier("main")
    private MovieCatalog movieCatalog3; // qualifier 为main的有两个bean，但是字段名movieCatalog3能够再次区分，所以注入成功

    /*@Autowired
    private MovieCatalog movieCatalog;*/ // error，qualifier 为main的有两个bean，但是字段名movieCatalog不能够再次区分，因此此处注入失败

    /**
     * 自引用注入：self references injection (that is, references back to the bean that is currently injected).
     * Note that self injection is a fallback. Regular dependencies on other components always have precedence.
     * In that sense, self references do not participate in regular candidate selection and are therefore in particular never primary.
     * On the contrary, they always end up as lowest precedence.
     * <p>
     * 因此，此处，beanId为movieRecommender1的会注入movieRecommender2，beanId为movieRecommender2的会注入movieRecommender1
     */
    @Autowired
    private MovieRecommender movieRecommender;
}
